package com.slg.android.listenbook.biz.bookcity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.slg.android.listenbook.R;
import com.slg.android.listenbook.biz.bookcity.adapter.CategoryListAdapter;
import com.slg.android.listenbook.biz.bookcity.model.BookInfo;
import com.slg.android.listenbook.biz.home.BaseActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;

public class CategoryListActivity extends BaseActivity implements AdapterView.OnItemClickListener {

    private LinkedList<BookInfo> data;
    private CategoryListAdapter adapter;
    private ListView listView;
    private String url;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_category_list);

        ImageView imgView = (ImageView) findViewById(R.id.title_bar_search_img);
        imgView.setVisibility(View.INVISIBLE);
        Intent intent = getIntent();
        String title = intent.getStringExtra("title");
        setTitle(title);
        String id = intent.getStringExtra("id");
        String category = intent.getStringExtra("list");
        if("category".equals(category)){
            url = "http://117.25.143.73/yyting/bookclient/ClientTypeResource.action?type="+id+"&pageNum=1&pageSize=500&token=DpPEKRjVAkk2j9DtIqO_H8vwQMcUBMNp-zdukwv7lbY*&imei=ODYwMzA4MDI4ODU1OTg1";
        }else{
            url = "http://117.25.143.77/yyting/bookclient/ClientRankingsItemList.action?rankType="+id+"&rangeType=3&pageNum=1&pageSize=100&token=DpPEKRjVAkk2j9DtIqO_H8vwQMcUBMNp-zdukwv7lbY**&imei=ODYwMzA4MDI4ODU1OTg1";
        }

        listView = (ListView) findViewById(R.id.category_list_activity);
        if(listView != null){
            data = new LinkedList<BookInfo>();
            adapter = new CategoryListAdapter(this, data);
            listView.setAdapter(adapter);

        }
        //网络获取数据名称
        HttpUtils httpUtils = new HttpUtils();

        httpUtils.send(HttpRequest.HttpMethod.GET,
                url
                , new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> objectResponseInfo) {

                        String result = objectResponseInfo.result;
                        LinkedList<BookInfo> list = new LinkedList<BookInfo>();
                        try {
                            JSONObject jsonObject = new JSONObject(result);
                            JSONArray jsonArray = jsonObject.getJSONArray("list");
                            for (int i = 0; i < jsonArray.length(); i++) {

                                JSONObject object = jsonArray.getJSONObject(i);
                                BookInfo bookInfo = new BookInfo();
                                bookInfo.parseJSON(object);
                                String cover = bookInfo.getCover();
                                System.out.print("cover"+cover);
                                list.add(bookInfo);
                            }
                            data.addAll(list);
                            adapter.notifyDataSetChanged();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                        Toast.makeText(CategoryListActivity.this, "网络出错", Toast.LENGTH_SHORT).show();
                    }
                });
        listView.setOnItemClickListener(this);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        BookInfo bookInfo = data.get(position);
        if(bookInfo != null){
            String typeId = bookInfo.getId();
            String bookName = bookInfo.getName();
            Intent intent = new Intent(this,PlayDetailActivity.class);
            intent.putExtra("bookId",typeId);
            intent.putExtra("bookName",bookName);
            startActivity(intent);

        }
    }
}
